<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $params = $request->only('phone','nickname','status');
        $res = User::getQueryData($params);
        return apiResponse(data: $res);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $params = $request->all();
        $params['register_ip'] = $request->getClientIp();
        $user = User::create($params);
        return apiResponse(0, '新增成功',$user);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $user = User::query()->findOrFail($id);
        return apiResponse(data: $user);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $user = User::query()->findOrFail($id);
        $user->update($request->all());
        return apiResponse(0,'更新成功',$user);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $ids)
    {
        User::destroy(explode(',',$ids));
        return apiResponse(msg: '删除成功');
    }
}
